package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.entity.RolePermission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lhh
 * @ClassName PermissionServiceImpl
 * @date 2022/9/29 11:41
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {
        //获取所有权限列表
        List<Permission> permissionList=permissionDao.findAll();

        //根据角色查询已分配的权限
        List<Long> permissionIdList=rolePermissionDao.findPermissionIdListByRoleId(roleId);

        //构建ztree数据
        //参考文档：http://www.treejs.cn/v3/demo.php#_201
        // { id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
        List<Map<String,Object>> zNodes = new ArrayList<Map<String,Object>>();
        for(Permission permission : permissionList) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id", permission.getId());
            map.put("pId", permission.getParentId());
            map.put("name", permission.getName());
            if(permissionIdList.contains(permission.getId())) {
                map.put("checked", true);
            }
            zNodes.add(map);
        }
        return zNodes;
    }

    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds) {
        //删除原有的权限
        rolePermissionDao.deleteByRoleId(roleId);

        for (Long permissionId : permissionIds) {
            if(!StringUtils.isEmpty(permissionId)){
                RolePermission permission = new RolePermission();
                permission.setRoleId(roleId);
                permission.setPermissionId(permissionId);
                rolePermissionDao.save(permission);
            }
        }
    }

    public List<Permission> findMenuPermissionByAdminId(Long adminId) {

        List<Permission> permissionList=null;
        if(adminId==1){
            permissionList= permissionDao.findAll();
        }else {
            permissionList=permissionDao.findListByAdminId(adminId);
        }
        List<Permission> result = PermissionHelper.bulid(permissionList);
        return result;
    }

    public List<Permission> findAllMenu() {
        //查询所有菜单
        List<Permission> permissionList = permissionDao.findAll();

        //判断菜单表是否为空
        if(StringUtils.isEmpty(permissionList)) {
            return  null;
        }
        List<Permission> result = PermissionHelper.bulid(permissionList);

        return result;
    }

    public List<String> findCodeListByAdminId(Long adminId) {
        if(adminId.longValue()==1){
            return  permissionDao.findAllCodeList();
        }
        return permissionDao.findCodeListByAdminId(adminId);
    }
}
